/**
 * @author Jianliang Chen
 *
 */
package com.xinpark.platform.cc.biz.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.xinpark.shared.ErrorCodeEnum;
import com.xinpark.shared.ServiceException;
import com.xinpark.shared.dal.DO.CategoryMainDO;
import com.xinpark.shared.dal.dao.CategoryMainDAO;
import com.xinpark.shared.service.CategoryService;
import com.xinpark.shared.service.convert.DalParamConvert;
import com.xinpark.shared.service.convert.ModelConverter;
import com.xinpark.shared.service.model.CategoryModel;
import com.xinpark.shared.service.param.QueryCategoryListParam;


public class CategoryServiceImpl implements CategoryService {
	@Autowired
    private CategoryMainDAO categoryDAO;

	private static List<CategoryModel> categories = null;
	
	/* (non-Javadoc)
	 * @see com.xinpark.shared.service.CategoryService#getCategoryList(com.xinpark.shared.service.param.QueryCategoryListParam)
	 */
	public List<CategoryModel> getCategoryList(QueryCategoryListParam param)
			throws ServiceException {
		
		if(null == param){
            throw new ServiceException(ErrorCodeEnum.INVALID_PARAM);
        }
        
        List<CategoryModel> categoryModelList = new ArrayList<CategoryModel>();
        
        try{
            List<CategoryMainDO> categoryDOList = categoryDAO.getCategoryList(DalParamConvert.queryCategoryListParam2Dal(param));
            
            for(CategoryMainDO categoryDO : categoryDOList){
                CategoryModel categoryModel = ModelConverter.categoryDO2Model(categoryDO);
                categoryModelList.add(categoryModel);
            }
        }catch(SQLException e){
            throw new ServiceException(ErrorCodeEnum.DB_EXCEPTION, e);
        } 
        
        return categoryModelList;
	}
	
	/* (non-Javadoc)
	 * @see com.xinpark.shared.service.CategoryService#getCategories()
	 */
	public List<CategoryModel> getAllCategories() 
		throws ServiceException {
		if(categories != null) {
			return categories;
		}
		
		categories = new ArrayList<CategoryModel> ();
		
		QueryCategoryListParam param = new QueryCategoryListParam();
		param.setSize(300);
		
		try{
            List<CategoryMainDO> categoryDOList = categoryDAO.getCategoryList(DalParamConvert.queryCategoryListParam2Dal(param));
            
            for(CategoryMainDO categoryDO : categoryDOList){
                CategoryModel categoryModel = ModelConverter.categoryDO2Model(categoryDO);
                categories.add(categoryModel);
            }
        }catch(SQLException e){
            throw new ServiceException(ErrorCodeEnum.DB_EXCEPTION, e);
        } 

		return categories;
	}

}
